package com.qsd.TeacherSystem.controller;

import cn.hutool.core.util.ObjectUtil;
import com.qsd.TeacherSystem.Service.TeacherService;
import com.qsd.common.enums.ResultStatus;
import com.qsd.common.po.Teacher;
import com.qsd.common.vo.BaseVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 用户登录
 * 用户注册
 * 跳转到用户编辑页,以及登录用户信息修改
 * <p>
 * create by it_mck·Miao
 */
@Slf4j
@RestController
@RequestMapping("teacher")
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    @GetMapping("login")
    public BaseVo login(String username, String password, HttpServletRequest request) {
        log.info("用户登录:用户名:{},密码:{}", username, password);
        if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(username)) {
            Teacher teacher = teacherService.login(username, password);
            if (ObjectUtil.isNotNull(teacher)) {
                request.getSession().setAttribute("teacher", teacher);
                return new BaseVo(ResultStatus.SUCCESS);
            }
            return new BaseVo(ResultStatus.PASSWORD_ERROR);
        }
        return new BaseVo(ResultStatus.USERNAME_OR_PASSWORD_MISS);
    }

    @PostMapping(value = "register")
    public BaseVo register(Teacher teacher, HttpServletRequest request) {
        log.info("用户注册:{}", teacher);
        Integer register = teacherService.register(teacher);
        if (register > 0) {
            request.getSession().setAttribute("teacher", teacher);
            return new BaseVo(ResultStatus.SUCCESS);
        }
        return new BaseVo(ResultStatus.REGISTER_ERROR);
    }

    @GetMapping("exit")
    public BaseVo exit(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("teacher");
        Object teacher = session.getAttribute("teacher");
        if (teacher == null) {
            return new BaseVo(ResultStatus.SUCCESS);
        }
        return new BaseVo(ResultStatus.SYSTEM_ERROR);
    }

    @GetMapping("edit")
    public ModelAndView edit(HttpServletRequest request) {
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        ModelAndView modelAndView = new ModelAndView("self_edit");
        log.info("跳转到修改当前用户信息页面:{}", teacher);
        modelAndView.addObject("teacher", teacher);
        return modelAndView;
    }

    @PostMapping(value = "/update")
    public BaseVo updateInfo(Teacher teacher, HttpServletRequest request) {
        log.info("用户信息修改:{}", teacher);
        boolean flag = teacherService.updateInfo(teacher,request);
        if (flag) {
            return new BaseVo(ResultStatus.SUCCESS);
        }
        return new BaseVo(ResultStatus.REGISTER_ERROR);
    }

    @PostMapping("/file")
    public BaseVo uploadFile(@RequestParam("file") MultipartFile multipartFile, String username, HttpServletRequest request) {
        log.info("更换头像,当前用户:{}", username);
        boolean flag = teacherService.uploadFileForHeader(multipartFile, username, request);
        if (flag) {
            return new BaseVo(ResultStatus.SUCCESS);
        }
        return new BaseVo(ResultStatus.HEADER_NET_ERROR);
    }

}
